<template>
  <div class="app-container">
    <div class="top-bar-container">
      <el-row>
        <el-col :span="24">
          <el-row type="flex" justify="end">
            <el-button
              type="primary"
              size="small"
              icon="el-icon-plus"
              @click="handleCreateRole"
            >新增角色
            </el-button>
          </el-row>
        </el-col>
      </el-row>
    </div>

    <el-table
      v-loading="listLoading"
      :data="list"
      border
      fit
      stripe
      highlight-current-row
    >
      <el-table-column align="center" label="ID">
        <template slot-scope="{ row }">
          <span>{{ row.id }}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" label="名称">
        <template slot-scope="{ row }">
          <span>{{ row.name }}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" label="描述">
        <template slot-scope="{ row }">
          <span>{{ row.description }}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" label="代码">
        <template slot-scope="{ row }">
          <span>{{ row.code }}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" label="系统保留">
        <template slot-scope="{ row }">
          <el-tag :type="row.isReserved === 1 ? 'success' : 'info'">
            {{ row.isReserved | yesNoStatusFilter }}
          </el-tag>
        </template>
      </el-table-column>

      <el-table-column align="center" label="创建时间">
        <template slot-scope="{ row }">
          <span>{{ row.createdTime }}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" label="更新时间">
        <template slot-scope="{ row }">
          <span>{{ row.updatedTime }}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" fixed="right" label="操作" width="280">
        <template slot-scope="scope">
          <el-button-group>
            <el-button
              type="primary"
              size="mini"
              icon="el-icon-share"
              title="分配权限"
              @click="handleAssignPermissionRow(scope.$index)"
            >分配权限
            </el-button>
            <el-button
              v-if="scope.row.isReserved !== 1"
              type="warning"
              size="mini"
              icon="el-icon-edit"
              title="编辑"
              @click="handleEditRoleRow(scope.$index)"
            >编辑
            </el-button>
            <el-button
              v-if="scope.row.isReserved !== 1"
              type="danger"
              size="mini"
              icon="el-icon-delete"
              title="删除"
              @click="handleDeleteRoleRow(scope.$index)"
            >删除
            </el-button>
          </el-button-group>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog
      :title="editForm.id ? '编辑角色' : '新增角色'"
      :visible.sync="dialogEditVisible"
      width="500px"
      @close="handleEditFormDialogClose"
    >
      <el-form
        ref="editForm"
        :model="editForm"
        :rules="editRules"
        class="edit-form"
        autocomplete="on"
        label-position="right"
      >
        <el-form-item prop="name" label="名称" label-width="60px">
          <el-input v-model="editForm.name" />
        </el-form-item>
        <el-form-item
          prop="description"
          label="描述"
          label-position="right"
          label-width="60px"
        >
          <el-input v-model="editForm.description" />
        </el-form-item>
        <el-form-item prop="code" label="代码" label-width="60px">
          <el-input v-model="editForm.code" />
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogEditVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleSaveRole">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  adminRoleCheckAvailable,
  adminRoleCreate,
  adminRoleDelete,
  adminRoleList,
  adminRoleUpdate
} from '@/api/rest/admin'
import { elRequiredRule } from '@/utils/el-validator'

const initForm = {
  id: null,
  name: null,
  description: null,
  code: null
}

export default {
  name: 'RoleList',
  data() {
    const validateCodeAvailable = (rule, value, callback) => {
      return adminRoleCheckAvailable(value, this.editForm.id).then((resp) => {
        if (!resp.data) {
          callback(new Error('角色代码已存在'))
        } else {
          callback()
        }
      })
    }

    return {
      list: null,
      listLoading: true,
      editForm: { ...initForm },
      editRules: {
        name: elRequiredRule,
        code: [
          elRequiredRule,
          { trigger: 'blur', validator: validateCodeAvailable }
        ]
      },
      dialogEditVisible: false
    }
  },
  created() {
    this.getRoleList()
  },
  methods: {
    getRoleList() {
      this.listLoading = true
      adminRoleList(this.listQuery).then((response) => {
        this.list = response.data
        this.listLoading = false
      })
    },
    handleEditRoleRow(index) {
      this.editForm = { ...this.list[index] }
      this.dialogEditVisible = true
    },
    handleDeleteRoleRow(index) {
      const data = this.list[index]
      this.$confirm('此操作将删除角色, 是否继续?', '提示', {
        type: 'warning'
      }).then(() => {
        adminRoleDelete(data.id)
          .then(() => {
            this.$message({
              type: 'success',
              message: '删除角色成功'
            })
            this.getRoleList()
          })
          .catch(() => {})
      })
    },
    handleCreateRole() {
      this.editForm = { ...initForm }
      this.dialogEditVisible = true
    },
    handleSaveRole() {
      this.$refs.editForm.validate((valid) => {
        if (valid) {
          if (this.editForm.id) {
            adminRoleUpdate(this.editForm).then((response) => {
              this.$message({
                message: '编辑角色成功',
                type: 'success'
              })
              this.getRoleList()
              this.dialogEditVisible = false
            })
          } else {
            adminRoleCreate(this.editForm).then((response) => {
              this.$message({
                message: '新增角色成功',
                type: 'success'
              })
              this.getRoleList()
              this.dialogEditVisible = false
            })
          }
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    handleEditFormDialogClose() {
      this.$refs.editForm.clearValidate()
    },
    handleAssignPermissionRow(index) {
      const { id } = this.list[index]
      this.$router.push({
        path: '/platform/role/assignPermission',
        query: { roleId: id }
      })
    }
  }
}
</script>
<style scoped></style>
